/**
 * @Author: LiMeng
 * @Date: 2017/5/31
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: ROOT_PATH + '/workLicense/apply/edit.shtml'
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            WindowManager.closeLayer({returnValue: true});
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
var addCommonFormParams = function (param) {
    param.workTypeName = $('#workTypeId').combo('getText');
    param.workLevelName = $('#workLevelId').combo('getText');
    param.deptName = $('#deptId').combobox('getText');
    param.applyDeptName = $('#applyDeptId').combobox('getText');
    param.id = GLOBAL.ID;
};
var formSubmitAction = function (url, params) {
    $.messager.progress();	// 显示进度条

    $('#form1').form('submit', {
        url: url,
        onSubmit: function (param) {
            addCommonFormParams(param);
            if ($.isNotEmpty(params)) {
                $.extend(param, params);
            }
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
            }
            return isValid;	// 返回false终止表单提交
        },
        success: function (result) {
            ajaxSuccessHandler(result);
        }
    });
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorAuditName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
};
var getSelectedApplyUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#applyUserName').textbox('setValue', names.substring(1));
    $('#applyUserId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
$(function () {

    $('#workTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.JOBTYPE,
        valueField: 'id',
        editable: false,
        required: true
    });

    $('#workLevelId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.WORK_LEVEL,
        valueField: 'id',
        editable: false,
        required: true
    });

    $('#contractorAuditName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['700px', '400px'],
                    content: ROOT_PATH + '/contractor/contractorAudit/auditedList.shtml',
                    callBack: getSelectedContractorAudit
                });
            }
        }],
        editable: false
    });

    $('#isCheckContractor').combobox({
        onSelect: function (record) {
            if (record.value === '0'){//部门
                $('#contractorAuditName').textbox('disable').parents('.form_one').addClass('hide');
                $('#contractorAuditId').prop('disabled', true);
                $('#deptId').textbox('enable').parents('.form_one').removeClass('hide');
            }else {//承包商
                $('#contractorAuditName').textbox('enable').parents('.form_one').removeClass('hide');
                $('#contractorAuditId').prop('disabled', false);
                $('#deptId').textbox('disable').parents('.form_one').addClass('hide');
            }
        }
    });

    $('#deptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#applyDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#applyUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择申请人', getSelectedApplyUser);
            }
        }],
        editable: false
    });

    $('#btn_save').on('click', function () {
        formSubmitAction(GLOBAL.ACTION_URL);
    });
});